package com.woniuxy.cloud.carl.user.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.carl.user.client.AddressClient;
import com.woniuxy.cloud.carl.user.client.param.AddAddressParam;
import com.woniuxy.cloud.carl.user.client.result.AddressDto;
import com.woniuxy.cloud.carl.user.common.UserResultCode;
import com.woniuxy.cloud.carl.user.infra.mapper.AddressesMapper;
import com.woniuxy.cloud.carl.user.infra.mapper.UserCarMapper;
import com.woniuxy.cloud.carl.user.infra.mapper.UserMapper;
import com.woniuxy.cloud.carl.user.infra.model.Addresses;
import com.woniuxy.cloud.carl.user.infra.model.User;
import com.woniuxy.cloud.carl.user.infra.model.UserCar;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.Result;
import com.woniuxy.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class AddressImpl implements AddressClient {
    @Resource
    private AddressesMapper addressesMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Result addNewAddress(AddAddressParam param) {

        User user = userMapper.selectById(param.getUserId());
        Assert.state(user!=null,"该用户不存在");
        Addresses addresses = BeanUtil.toBean(param, Addresses.class);
        List<Addresses> addresses1 = addressesMapper.selectList(null);
        if (addresses1.size() == 0) {
            addresses.setDefaultAddr(1);
        } else {
            addresses.setDefaultAddr(0);
        }
        addressesMapper.insert(addresses);
        return Result.success();
    }

    @Override
    public Result<List<AddressDto>>findAll(Integer userId) {

        User user = userMapper.selectById(userId);
        Assert.state(user!=null,"该用户不存在");
        QueryWrapper<Addresses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Addresses> addresses = addressesMapper.selectList(queryWrapper);
        List<AddressDto> addressDtos = BeanUtil.copyToList(addresses, AddressDto.class);
        return Result.success(addressDtos);
    }

    @Override
    public Result<AddressDto> lookDefault(Integer userId) {
        User user = userMapper.selectById(userId);
        Assert.state(user!=null,"该用户不存在");
        QueryWrapper<Addresses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("default_addr",1);
        Addresses address = addressesMapper.selectOne(queryWrapper);
        AddressDto addressDto = BeanUtil.toBean(address, AddressDto.class);
        return Result.success(addressDto);
    }

    @Override
    public Result setDefault(Long addressId) {
        Addresses addresses = addressesMapper.selectById(addressId);
        Assert.state(addresses!=null,"不存在该地址");//验证该地址是否存在
        Integer userId = addresses.getUserId();//找到该收获地址的用户id
        QueryWrapper<Addresses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("default_addr",1);
        Addresses beforeDefault = addressesMapper.selectOne(queryWrapper);//得到原来的默认地址
        if(beforeDefault!=null) {
            if (beforeDefault.getAddressId() == addressId) {
                throw new MallException(UserResultCode.ADDRESS_DOUBLE_DEFAULT);
            }
//以前默认地址存在的情况
            beforeDefault.setDefaultAddr(0);
            addressesMapper.updateById(beforeDefault);
            addresses.setDefaultAddr(1);
            addressesMapper.updateById(addresses);
        }else {//以前默认地址不存在的情况
            addresses.setDefaultAddr(1);
            addressesMapper.updateById(addresses);
        }
        return Result.success();
    }

    @Override
    public Result deleteAdddress(Long addressId) {
        Addresses addresses = addressesMapper.selectById(addressId);
        Assert.state(addresses!=null,"不存在该地址");
        addressesMapper.deleteById(addresses);
        return Result.success();
    }

    @Override
    public Result<AddressDto> findDefaultAddress(Long userId) {
        QueryWrapper<Addresses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("default_addr",1);
        Addresses addresses = addressesMapper.selectOne(queryWrapper);
        AddressDto addressDto = BeanUtil.toBean(addresses, AddressDto.class);
        return Result.success(addressDto);
    }
}
